package cn.webrx.servlet;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>功能描述: </p>
 *
 * @author webrx
 * @version 1.0
 * @date 2020-06-09 14:39
 */
@WebServlet("/check")
public class Check extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject su = SecurityUtils.getSubject();
        try {
            su.login(token);
        } catch (UnknownAccountException uae) {
            System.out.println("账号不正确");
        } catch (LockedAccountException lae) {
            System.out.println("账号被锁定");
        } catch (DisabledAccountException dae) {
            System.out.println("账号被禁用");
        } catch (IncorrectCredentialsException e) {
            System.out.println("密码错误");
        } catch (Exception exc) {
            exc.printStackTrace();
        }
        if (su.isAuthenticated()) {
            System.out.println("登录认证成功");
            System.out.println(su.getPrincipal());
        }
    }
}
